package com.surelution.projects.crm

import java.text.SimpleDateFormat

class NoGasPlanSumController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def sdf = new SimpleDateFormat('yyyy-MM-dd')
		def dateFrom = params.dateFrom? sdf.parse(params.dateFrom):null
		def dateTo = params.dateTo? sdf.parse(params.dateTo):null
		def station = params['station.id']?OwnStation.get(params['station.id']):null
		def result = NoGasPlanSum.createCriteria().list ([max:params.max, offset:params.offset]){
			if(station){
				eq("station",station)
			}
			if(dateFrom) {
				ge("planTime", dateFrom)
			}
			if(dateTo) {
				le("planTime", dateTo)
			}
			order("planTime","desc")
		}
        [noGasPlanSumInstanceList: result, noGasPlanSumInstanceTotal: result.totalCount]
    }

    def create = {
        def noGasPlanSumInstance = new NoGasPlanSum()
        noGasPlanSumInstance.properties = params
        return [noGasPlanSumInstance: noGasPlanSumInstance]
    }

    def save = {
        def noGasPlanSumInstance = new NoGasPlanSum(params)
        if (noGasPlanSumInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum'), noGasPlanSumInstance.id])}"
            redirect(action: "show", id: noGasPlanSumInstance.id)
        }
        else {
            render(view: "create", model: [noGasPlanSumInstance: noGasPlanSumInstance])
        }
    }

    def show = {
        def noGasPlanSumInstance = NoGasPlanSum.get(params.id)
        if (!noGasPlanSumInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum'), params.id])}"
            redirect(action: "list")
        }
        else {
            [noGasPlanSumInstance: noGasPlanSumInstance]
        }
    }

    def edit = {
        def noGasPlanSumInstance = NoGasPlanSum.get(params.id)
        if (!noGasPlanSumInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [noGasPlanSumInstance: noGasPlanSumInstance]
        }
    }

    def update = {
        def noGasPlanSumInstance = NoGasPlanSum.get(params.id)
        if (noGasPlanSumInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (noGasPlanSumInstance.version > version) {
                    
                    noGasPlanSumInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum')] as Object[], "Another user has updated this NoGasPlanSum while you were editing")
                    render(view: "edit", model: [noGasPlanSumInstance: noGasPlanSumInstance])
                    return
                }
            }
            noGasPlanSumInstance.properties = params
            if (!noGasPlanSumInstance.hasErrors() && noGasPlanSumInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum'), noGasPlanSumInstance.id])}"
                redirect(action: "show", id: noGasPlanSumInstance.id)
            }
            else {
                render(view: "edit", model: [noGasPlanSumInstance: noGasPlanSumInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def noGasPlanSumInstance = NoGasPlanSum.get(params.id)
        if (noGasPlanSumInstance) {
            try {
                noGasPlanSumInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'noGasPlanSum.label', default: 'NoGasPlanSum'), params.id])}"
            redirect(action: "list")
        }
    }
}
